﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gtkj.CMS.Common
{
    public class PageHelper
    {
        #region 分页控件生成方法
        /// <summary>
        /// 分页控件生成方法
        /// </summary>
        /// <param name="param">基础参数</param>
        /// <returns>分页控件HTML</returns>
        public static string GetPageBar(BaseParam param)
        {
            int t_pagecount = (param.TotalCount + param.PageSize - 1) / param.PageSize;
            param.PageCount = t_pagecount;

            //只有当前一页的情况返回空
            if (1 == t_pagecount)
            {
                return string.Empty;
            }

            //页码起始位置
            int t_start = param.PageIndex - 5;
            if (t_start < 1)
            {
                t_start = 1;
            }
            //页码结束位置
            int t_end = t_start + 9;
            if (t_end > t_pagecount)
            {
                t_end = t_pagecount;
                t_start = t_end - 9 > 1 ? t_end - 9 : 1;
            }

            //搭建分页控件Html
            StringBuilder t_stringbulider = new StringBuilder();

            if (param.PageIndex > 1)
            {
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>首页</a></li>", 1));
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>上一页</a></li>", param.PageIndex - 1));
            }

            for (int temp = t_start; temp <= t_end; temp++)
            {
                if (temp == param.PageIndex)
                {
                    t_stringbulider.Append("<li class='page_hover'><a>" + temp + "</a></li>");
                }
                else
                {
                    t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>{0}</a></li>", temp));
                }
            }

            if (param.PageIndex < t_pagecount)
            {
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>下一页</a></li>", param.PageIndex + 1));
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>尾页</a></li>", t_pagecount));
            }
            string t_textbox = "<input type='textbox' style='width:30px' id='jumpindex' />";
            string t_button = string.Format("<input type='button' style='width:40px' value='跳转' onclick='{0}' />", "GoPage($(\"#jumpindex\").val())");
            t_stringbulider.Append(string.Format("<div class='page'>&nbsp&nbsp到:{0}页{1}", t_textbox, t_button));
            t_stringbulider.Append(string.Format("&nbsp&nbsp页码:<span>{0}/{1}</span>", param.PageIndex, t_pagecount));
            t_stringbulider.Append(string.Format("&nbsp&nbsp总条数:<span>{0}</span></div>", param.TotalCount));
            return t_stringbulider.ToString();
        }
        #endregion

        #region 分页控件生成方法dl/dd
        /// <summary>
        /// 分页控件生成方法
        /// </summary>
        /// <param name="param">基础参数</param>
        /// <returns>分页控件HTML</returns>
        public static string GetPageBar2(BaseParam param)
        {
            int t_pagecount = (param.TotalCount + param.PageSize - 1) / param.PageSize;
            param.PageCount = t_pagecount;

            //只有当前一页的情况返回空
            if (1 == t_pagecount)
            {
                return string.Empty;
            }

            //页码起始位置
            int t_start = param.PageIndex - 5;
            if (t_start < 1)
            {
                t_start = 1;
            }
            //页码结束位置
            int t_end = t_start + 9;
            if (t_end > t_pagecount)
            {
                t_end = t_pagecount;
                t_start = t_end - 9 > 1 ? t_end - 9 : 1;
            }

            //搭建分页控件Html
            StringBuilder t_stringbulider = new StringBuilder();

            if (param.PageIndex > 1)
            {
                t_stringbulider.Append(string.Format("<dd><a href='javascript:void({0})'>上一页</a></dd>", param.PageIndex - 1));
            }

            for (int temp = t_start; temp <= t_end; temp++)
            {
                if (temp == param.PageIndex)
                {
                    t_stringbulider.Append("<dd class='page_hover'><a>" + temp + "</a></dd>");
                }
                else
                {
                    t_stringbulider.Append(string.Format("<dd><a href='javascript:void({0})'>{0}</a></dd>", temp));
                }
            }

            if (param.PageIndex < t_pagecount)
            {
                t_stringbulider.Append(string.Format("<dd><a href='javascript:void({0})'>下一页</a></dd>", param.PageIndex + 1));
            }
            return t_stringbulider.ToString();
        }
        #endregion

        #region 分页控件生成方法不带跳转
        /// <summary>
        /// 分页控件生成方法不带跳转
        /// </summary>
        /// <param name="param">基础参数</param>
        /// <returns>分页控件HTML</returns>
        public static string GetPageBar3(BaseParam param)
        {
            int t_pagecount = (param.TotalCount + param.PageSize - 1) / param.PageSize;
            param.PageCount = t_pagecount;

            //只有当前一页的情况返回空
            if (1 == t_pagecount)
            {
                return string.Empty;
            }

            //页码起始位置
            int t_start = param.PageIndex - 5;
            if (t_start < 1)
            {
                t_start = 1;
            }
            //页码结束位置
            int t_end = t_start + 9;
            if (t_end > t_pagecount)
            {
                t_end = t_pagecount;
                t_start = t_end - 9 > 1 ? t_end - 9 : 1;
            }

            //搭建分页控件Html
            StringBuilder t_stringbulider = new StringBuilder();

            if (param.PageIndex > 1)
            {
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>首页</a></li>", 1));
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>上一页</a></li>", param.PageIndex - 1));
            }

            for (int temp = t_start; temp <= t_end; temp++)
            {
                if (temp == param.PageIndex)
                {
                    t_stringbulider.Append("<li class='page_hover'><a>" + temp + "</a></li>");
                }
                else
                {
                    t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>{0}</a></li>", temp));
                }
            }

            if (param.PageIndex < t_pagecount)
            {
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>下一页</a></li>", param.PageIndex + 1));
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>尾页</a></li>", t_pagecount));
            }
            return t_stringbulider.ToString();
        }
        #endregion

        #region 分页控件生成方法不带跳转
        /// <summary>
        /// 分页——前台列表页
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string GetPageBar4(BaseParam param)
        {
            int t_pagecount = (param.TotalCount + param.PageSize - 1) / param.PageSize;
            param.PageCount = t_pagecount;

            //只有当前一页的情况返回空
            if (1 == t_pagecount)
            {
                return string.Empty;
            }

            //页码起始位置
            int t_start = param.PageIndex - 5;
            if (t_start < 1)
            {
                t_start = 1;
            }
            //页码结束位置
            int t_end = t_start + 9;
            if (t_end > t_pagecount)
            {
                t_end = t_pagecount;
                t_start = t_end - 9 > 1 ? t_end - 9 : 1;
            }

            //搭建分页控件Html
            StringBuilder t_stringbulider = new StringBuilder();

            if (param.PageIndex > 1)
            {
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>首页</a></li>", 1));
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>上一页</a></li>", param.PageIndex - 1));
            }

            for (int temp = t_start; temp <= t_end; temp++)
            {
                if (temp == param.PageIndex)
                {
                    t_stringbulider.Append("<li class='page_h'><a>" + temp + "</a></li>");
                }
                else
                {
                    t_stringbulider.Append(string.Format("<li class='page_m'><a href='javascript:void({0})'>{0}</a></li>", temp));
                }
            }

            if (param.PageIndex < t_pagecount)
            {
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>下一页</a></li>", param.PageIndex + 1));
                t_stringbulider.Append(string.Format("<li><a href='javascript:void({0})'>尾页</a></li>", t_pagecount));
            }
            return t_stringbulider.ToString();
        }
        #endregion


        #region 分页控件生成方法 个人中心
        /// <summary>
        /// 分页——前台列表页
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string GetPageBar5(BaseParam param)
        {
            int t_pagecount = (param.TotalCount + param.PageSize - 1) / param.PageSize;
            param.PageCount = t_pagecount;

            //只有当前一页的情况返回空
            if (1 == t_pagecount)
            {
                return string.Empty;
            }

            //页码起始位置
            int t_start = param.PageIndex - 5;
            if (t_start < 1)
            {
                t_start = 1;
            }
            //页码结束位置
            int t_end = t_start + 9;
            if (t_end > t_pagecount)
            {
                t_end = t_pagecount;
                t_start = t_end - 9 > 1 ? t_end - 9 : 1;
            }
            //<dl>
            //     <dd class="page_m"><a href="">&lt;</a></dd>
            //     <dd class="page_m" id="page_h"><a href="">1</a></dd>
            //     <dd class="page_m"><a href="">2</a></dd>
            //     <dd class="page_m"><a href="">3</a></dd>
            //     <dd class="page_m"><a href="">4</a></dd>
            //     <dd>…</dd>
            //     <dd class="page_m"><a href="">100</a></dd>
            //     <dd class="page_m"><a href="">&gt;</a></dd>
            //     <dd>共100页，到第</dd>
            //     <dd><input type="text" class="page_t" />页</dd>
            //     <dd><input type="button" class="page_d" value="跳转" /></dd>
            //     <div class="cl"></div>
            // </dl>
            //搭建分页控件Html
            StringBuilder t_stringbulider = new StringBuilder();
            t_stringbulider.Append("<dl>");
            if (param.PageIndex > 1)
            {
                t_stringbulider.Append(string.Format("<dd class='page_m'><a href='javascript:void({0})'>首页</a></dd>", 1));
                t_stringbulider.Append(string.Format("<dd class='page_m'><a href='javascript:void({0})'><</a></dd>", param.PageIndex - 1));
            }

            for (int temp = t_start; temp <= t_end; temp++)
            {
                if (temp == param.PageIndex)
                {
                    t_stringbulider.Append("<dd class='page_m' id='page_h'><a>" + temp + "</a></dd>");
                }
                else
                {
                    t_stringbulider.Append(string.Format("<dd class=\"page_m\"><a href='javascript:void({0})'>{0}</a></dd>", temp));
                }
            }

            if (param.PageIndex < t_pagecount)
            {
                t_stringbulider.Append(string.Format("<dd class='page_m'><a href='javascript:void({0})'>></a></dd>", param.PageIndex + 1));
                t_stringbulider.Append(string.Format("<dd class='page_m'><a href='javascript:void({0})'>尾页</a></dd>", t_pagecount));
            }
            t_stringbulider.Append("</dl>");
            return t_stringbulider.ToString();
        }
        #endregion

        #region 分页控件生成方法 游戏列表
        /// <summary>
        /// 分页——前台列表页
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string GetPageBar6(BaseParam param)
        {
            int pagecount = (param.TotalCount + param.PageSize - 1) / param.PageSize;
            param.PageCount = pagecount;

            //只有当前一页的情况返回空
            if (1 == pagecount)
            {
                return string.Empty;
            }

            //页码起始位置
            int start = param.PageIndex - 5;
            if (start < 1)
            {
                start = 1;
            }
            //页码结束位置
            int end = start + 4;
            if (end > pagecount)
            {
                end = pagecount;
                start = end - 4 > 1 ? end - 4 : 1;
            }
            //<dl>
            //         <dd class="page_m"><a href="javascript:void(0)">&lt;</a></dd>
            //         <dd class="page_m" id="page_h"><a href="javascript:void(0)">1</a></dd>
            //         <dd class="page_m"><a href="javascript:void(0)">2</a></dd>
            //         <dd class="page_m"><a href="javascript:void(0)">3</a></dd>
            //         <dd class="page_m"><a href="javascript:void(0)">4</a></dd>
            //         <dd>…</dd>
            //         <dd class="page_m"><a href="javascript:void(0)">100</a></dd>
            //         <dd class="page_m"><a href="javascript:void(0)">&gt;</a></dd>
            //         <dd>共100页，到第</dd>
            //         <dd><input type="text" class="page_t" />页</dd>
            //         <dd><input type="button" class="page_d" value="跳转" /></dd>
            //         <div class="cl"></div>
            //     </dl>
            //搭建分页控件Html
            StringBuilder stringbulider = new StringBuilder();
            if (pagecount > 0)
            {
                stringbulider.Append("<dl>");
            }
            if (param.PageIndex > 1)
            {
                stringbulider.Append(string.Format("<dd class='page_m'><a href='javascript:void({0})'>首页</a></dd>", 1));
                stringbulider.Append(string.Format("<dd class='page_m'><a href='javascript:void({0})'>&lt;</a></dd>", param.PageIndex - 1));
            }

            for (int temp = start; temp <= end; temp++)
            {
                if (temp == param.PageIndex)
                {
                    stringbulider.Append("<dd class='page_m' id='page_h'><a>" + temp + "</a></dd>");
                }
                else
                {
                    stringbulider.Append(string.Format("<dd class=\"page_m\"><a href='javascript:void({0})'>{0}</a></dd>", temp));
                }
            }
            if (end < pagecount)
            {
                stringbulider.Append(string.Format("<dd>…</dd>"));
                stringbulider.Append(string.Format("<dd class='page_m'><a href='javascript:void({0})'>{0}</a></dd>", pagecount));
            }

            if (param.PageIndex < pagecount)
            {
                stringbulider.Append(string.Format("<dd class='page_m'><a href='javascript:void({0})'>&gt;</a></dd>", param.PageIndex + 1));
                stringbulider.Append(string.Format("<dd class='page_m'><a href='javascript:void({0})'>尾页</a></dd>", pagecount));

            }
            if (param.PageCount > 0)
            {
                stringbulider.Append(string.Format("<dd>第</dd>"));
                stringbulider.Append(string.Format("<dd><input type='text' class='page_t' />页</dd>", pagecount));
                stringbulider.Append(string.Format("<dd><input type='button' class='page_d' value='跳转' /></dd>", pagecount));
                stringbulider.Append("</dl>");
            }

            return stringbulider.ToString();
        }
        #endregion

    }

    public class BaseParam
    {
        string keyword;
        /// <summary>
        /// 当前页面编号
        /// </summary>
        public int PageIndex { get; set; }
        /// <summary>
        /// 页面大小
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 总条数
        /// </summary>
        public int TotalCount
        {
            get;
            set;
        }
        /// <summary>
        /// 起始行
        /// </summary>
        public int StartIndex { get; set; }
        /// <summary>
        /// 结束行
        /// </summary>
        public int EndIndex { get; set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount { get; set; }
        /// <summary>
        /// 关键字
        /// </summary>
        public string KeyWord
        {
            get
            {
                if (string.IsNullOrWhiteSpace(keyword))
                    return null;
                return keyword.Trim();
            }
            set{keyword=value;}
        }
        /// <summary>
        /// 排序字段
        /// </summary>
        public string Sort { get; set; }
        /// <summary>
        /// asc or desc
        /// </summary>
        public bool Order { get; set; }

        public BaseParam()
        {
            this.TotalCount = 0;
            this.PageIndex = 1;
            this.PageSize = 20;

        }
    }

}
